﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using deezer.sdk.windowsphone;


namespace PhoneAppTest
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            List<TypeOfGet> dataListBox = new List<TypeOfGet>();
            dataListBox.Add(new TypeOfGet{Id = 0, Label = "Album"});
            dataListBox.Add(new TypeOfGet{Id = 1, Label = "Artiste"});
            dataListBox.Add(new TypeOfGet{Id = 2, Label = "Genre"});
            dataListBox.Add(new TypeOfGet{Id = 3, Label = "Search" });
            dataListBox.Add(new TypeOfGet { Id = 4, Label = "Editorial" });

            dataListBox.Add(new TypeOfGet { Id = 5, Label = "Album_Comments" });
            dataListBox.Add(new TypeOfGet { Id = 6, Label = "Album_Fans" });
            dataListBox.Add(new TypeOfGet { Id = 7, Label = "Album_Tracks" });

            dataListBox.Add(new TypeOfGet { Id = 8, Label = "Artist_Top" });
            dataListBox.Add(new TypeOfGet { Id = 9, Label = "Artist_Albums" });
            dataListBox.Add(new TypeOfGet { Id = 10, Label = "Artist_Comments" });
            dataListBox.Add(new TypeOfGet { Id = 11, Label = "Artist_Fans" });
            dataListBox.Add(new TypeOfGet { Id = 12, Label = "Artist_Related" });
            dataListBox.Add(new TypeOfGet { Id = 13, Label = "Artist_Radio" });

            listBoxType.ItemsSource = dataListBox;
            listBoxType.DisplayMemberPath = "Label";
            listBoxType.FontSize = 30;
        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            TypeOfGet valueListBox = (TypeOfGet)listBoxType.SelectedItem;
            switch (valueListBox.Id)
            {
                case 0:
                    GetAlbum();
                    break;
                case 1:
                    GetArtist();
                    break;
                case 2:
                    GetGenre();
                    break;
                case 3:
                    GetSearch();
                    break;
                case 4:
                    GetEditorial();
                    break;
                case 5:
                    GetAlbumComments();
                    break;
                case 6:
                    GetAlbumFans();
                    break;
                case 7:
                    GetAlbumTracks();
                    break;
                case 8:
                    GetArtistTop();
                    break;

            }
        }

        #region GetSearch()

        private void GetSearch()
        {
            Search search = new Search();
            search.SearchResponse += new SearchResponseEventHandler(search_SearchResponse);
            search.SearchAsync(textBoxParams.Text);
        }

        void search_SearchResponse(object sender, EventArgsSearch e)
        {
            String valueToDisplay = String.Empty;

            foreach (TypeTrack item in e.Response().Tracks)
            {
                valueToDisplay += "Tracks : " + item.Id + " : " + item.Title + Environment.NewLine;
            }

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                textBoxResult.Text = valueToDisplay;
            });
        }

        #endregion

        #region GetAlbum()

        private void GetAlbum()
        {
            textBoxResult.Text = String.Empty;
            Album getAlbumEminem = new Album();
            getAlbumEminem.GetAlbumByIdResponse += new GetAlbumByIdResponseEventHandler(getAlbumEminem_GetAlbumByIdResponse);
            getAlbumEminem.GetAlbumByIdAsync(Convert.ToInt32(textBoxParams.Text));
        }

        void getAlbumEminem_GetAlbumByIdResponse(object sender, EventArgsAlbum e)
        {
            String valueToDisplay = "Id : " + e.Response().Id + Environment.NewLine +
                                    "Artist : " + e.Response().Artist.Name + Environment.NewLine +
                                    "Title : " + e.Response().Title + Environment.NewLine +
                                    "Link : " + e.Response().Link + Environment.NewLine +
                                    "Cover : " + e.Response().Cover + Environment.NewLine +
                                    "Label : " + e.Response().Label + Environment.NewLine +
                                    "Duration : " + e.Response().Duration + Environment.NewLine +
                                    "Fans : " + e.Response().Fans + Environment.NewLine +
                                    "Release_Date : " + e.Response().Release_Date + Environment.NewLine;

            foreach(TypeTrack item in e.Response().Tracks)
            {
                valueToDisplay += "Tracks : " + item.Id + " : " + item.Title + Environment.NewLine;
            }

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                textBoxResult.Text = valueToDisplay;
            });
        }
        
        #endregion GetAlbum()

        #region GetArtist()

        private void GetArtist()
        {
            textBoxResult.Text = String.Empty;
            Artist getArtist = new Artist();
            getArtist.GetArtistByIdResponse +=new GetArtistByIdResponseEventHandler(getArtist_GetArtistByIdResponse);
            getArtist.GetArtistByIdAsync(Convert.ToInt32(textBoxParams.Text));
        }

        void getArtist_GetArtistByIdResponse(object sender, EventArgsArtist e)
        {
            TypeArtist tmp = new TypeArtist();
            String valueToDisplay = "Id : " + e.Response().Id + Environment.NewLine +
                        "Name : " + e.Response().Name + Environment.NewLine +
                        "Link : " + e.Response().Link + Environment.NewLine +
                        "Picture : " + e.Response().Picture + Environment.NewLine +
                        "Nb_album : " + e.Response().Nb_album + Environment.NewLine +
                        "Nb_fan : " + e.Response().Nb_fan + Environment.NewLine +
                        "Radio : " + e.Response().Radio + Environment.NewLine +
                        "Type : " + e.Response().Type + Environment.NewLine;

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                textBoxResult.Text = valueToDisplay;
            });

        }

        #endregion GetArtist()

        #region GetGenre()

        private void GetGenre()
        {
            textBoxResult.Text = String.Empty;
            Genre getGenre = new Genre();
            getGenre.GetGenreResponse += new GetGenreResponseEventHandler(getGenre_GetGenreResponse);
            getGenre.GetGenreAsync(Convert.ToInt32(textBoxParams.Text));
        }

        void getGenre_GetGenreResponse(object sender, EventArgsGenre e)
        {
            String valueToDisplay = String.Empty;

            foreach(TypeGenre item in e.Response())
            {
                valueToDisplay += "Id : " + item.Id + Environment.NewLine +
                        "Name : " + item.Name + Environment.NewLine +
                        "Type : " + item.Type + Environment.NewLine;
                valueToDisplay += "====================================" + Environment.NewLine;

            }
                

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                textBoxResult.Text = valueToDisplay;
            });
        }

        #endregion GetGenre()

        #region GetAlbumComments()
        private void GetAlbumComments()
        {
            textBoxResult.Text = String.Empty;
            Album instAlbum = new Album();
            instAlbum.GetCommentsAlbumByIdResponse += new GetCommentsAlbumByIdResponseEventHandler(instAlbum_GetCommentsAlbumByIdResponse);
            instAlbum.GetCommentsAlbumByIdAsync(Convert.ToInt32(textBoxParams.Text));
        }

        void instAlbum_GetCommentsAlbumByIdResponse(object sender, EventArgsCommentsAlbum e)
        {
            String valueToDisplay = String.Empty;

            foreach (TypeComment item in e.Response().Comments)
            {
                valueToDisplay += "Id : " + item.Id + Environment.NewLine +
                        "Author : " + item.Author.Name + Environment.NewLine +
                        "Texte : " + item.Text + Environment.NewLine;
                valueToDisplay += "====================================" + Environment.NewLine;

            }


            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                textBoxResult.Text = valueToDisplay;
            });
        }
        
        #endregion

        #region GetAlbumFans()
        private void GetAlbumFans()
        {
            textBoxResult.Text = String.Empty;
            Album instAlbum = new Album();
            instAlbum.GetFansAlbumByIdResponse += new GetFansAlbumByIdResponseEventHandler(instAlbum_GetFansAlbumByIdResponse);
            instAlbum.GetFansAlbumByIdAsync(Convert.ToInt32(textBoxParams.Text));
        
        }

        void instAlbum_GetFansAlbumByIdResponse(object sender, EventArgsFansAlbum e)
        {
            String valueToDisplay = String.Empty;

            foreach (TypeUser item in e.Response().Users)
            {
                valueToDisplay += "Id : " + item.Id + Environment.NewLine +
                        "Name : " + item.Name + Environment.NewLine +
                        "Link : " + item.Link + Environment.NewLine +
                        "Picture : " + item.Picture + Environment.NewLine;
                valueToDisplay += "====================================" + Environment.NewLine;

            }


            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                textBoxResult.Text = valueToDisplay;
            });
        }

        #endregion

        #region GetAlbumTracks()
        private void GetAlbumTracks()
        {
            textBoxResult.Text = String.Empty;
            Album instAlbum = new Album();
            instAlbum.GetTracksAlbumByIdResponse += new GetTracksAlbumByIdResponseEventHandler(instAlbum_GetTracksAlbumByIdResponse);
            instAlbum.GetTracksAlbumByIdAsync(Convert.ToInt32(textBoxParams.Text));

        }

        void instAlbum_GetTracksAlbumByIdResponse(object sender, EventArgsTracksAlbum e)
        {
            String valueToDisplay = String.Empty;

            foreach (TypeTrack item in e.Response().Tracks)
            {
                valueToDisplay += "Id : " + item.Id + Environment.NewLine +
                        "Readable : " + item.Readable + Environment.NewLine +
                        "Title : " + item.Title + Environment.NewLine +
                        "Link : " + item.Link + Environment.NewLine +
                        "Duration : " + item.Duration + Environment.NewLine +
                        "Rank : " + item.Rank + Environment.NewLine +
                        "Preview : " + item.Preview + Environment.NewLine +
                        "Artist : " + item.Artist.Name + Environment.NewLine;
                valueToDisplay += "====================================" + Environment.NewLine;

            }


            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                textBoxResult.Text = valueToDisplay;
            });
        }
        #endregion

        #region GetEditorial()

        private void GetEditorial()
        {
            textBoxResult.Text = String.Empty;
            Editorial getGenre = new Editorial();
            getGenre.GetEditorialResponse += new GetEditorialResponseEventHandler(getGenre_GetEditorialResponse);
            getGenre.GetEditorialAsync(Convert.ToInt32(textBoxParams.Text));
        }

        void getGenre_GetEditorialResponse(object sender, EventArgsEditorial e)
        {
            String valueToDisplay = String.Empty;

            foreach (TypeEditorial item in e.Response())
            {
                valueToDisplay += "Id : " + item.Id + Environment.NewLine +
                        "Name : " + item.Name + Environment.NewLine +
                        "Type : " + item.Type + Environment.NewLine;
                valueToDisplay += "====================================" + Environment.NewLine;

            }


            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                textBoxResult.Text = valueToDisplay;
            });
        }

        #endregion GetEditorial()


        #region GetArtistTop()

        private void GetArtistTop()
        {
            textBoxResult.Text = String.Empty;
            Artist getArtist = new Artist();
            getArtist.GetTopArtistByIdResponse += new GetTopArtistByIdResponseEventHandler(getArtist_GetTopArtistByIdResponse);
            getArtist.GetTopArtistByIdAsync(Convert.ToInt32(textBoxParams.Text));
        }

        void getArtist_GetTopArtistByIdResponse(object sender, EventArgsTopArtist e)
        {
            String valueToDisplay = String.Empty;

            foreach (TypeTrack item in e.Response().Tracks)
            {
                valueToDisplay += "Id : " + item.Id + Environment.NewLine +
                        "Readable : " + item.Readable + Environment.NewLine +
                        "Title : " + item.Title + Environment.NewLine +
                        "Link : " + item.Link + Environment.NewLine +
                        "Duration : " + item.Duration + Environment.NewLine +
                        "Preview : " + item.Preview + Environment.NewLine +
                        "Artist : " + item.Artist.Name + Environment.NewLine +
                        "Type : " + item.Type + Environment.NewLine;
                valueToDisplay += "====================================" + Environment.NewLine;

            }


            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                textBoxResult.Text = valueToDisplay;
            });
        }

        #endregion
    }
}